//
//  OosReviewEntryParser.m
//  oos4ios
//
//  Created by Pablo Gonzalez on 4/25/11.
//  Copyright 2011 11870.com. All rights reserved.
//

#import "OosReviewEntryParser.h"
#import <oos4ios/Model/OosUser.h>
#import <oos4ios/Model/OosAvatarImage.h>
#import <oos4ios/Model/OosPrivacy.h>
#import "OosServiceEntryParser.h"
#import "../Atom/Entries/OosServiceEntry.h"
#import <oos4ios/Oos4ios.h>
#import <oos4ios/Model/OosReview.h>
#import "../GData/Common/Elements/GDataCategory.h"
#import "../Utils/OosEnum+Internal.h"


@implementation OosReviewEntryParser

static OosReviewEntryParser *instance;

+ (OosReviewEntryParser *)instance
{
    if (!instance) {
        instance = [OosReviewEntryParser new];
    }
    return instance;
}

- (id)parseEntry:(OosEntry *)rawEntry
{
    OosServiceEntry *entry = (OosServiceEntry *)rawEntry;
    
    NSString *userSlug = entry.author.slug;
    
    if (!userSlug) {
        userSlug = [NSString stringWithFormat:@"pro/%@", 
                    [entry.slug stringValue], nil];
    }
    
    OosUser *user = [OosUser userFromSlug:userSlug];
	user.nick = entry.author.name;
	if (!user.avatar) {
		user.avatar = [OosAvatarImage create];
		user.avatar.urlString = entry.author.avatarUrl;
	}
	
	OosReview *review = [OosReview reviewFromServiceSlug:[entry.slug stringValue] 
											 andUserSlug:user.slug];
	NSDate *updatedDate = [entry.updatedDate date];
    
	
    // get tags and lists
    review.listsString = [entry.lists stringValue];
    review.tagsString = [entry.tags stringValue];
	
	if (!review.updated
		|| [review.updated compare: updatedDate] == NSOrderedAscending) {
		review.author = user;
		review.title = [entry.summary stringValue];
        NSString *content = [entry.content stringValue];
        content = [content stringByReplacingOccurrencesOfString:@"&#13;" withString:@""];
		review.content = content;
		review.rating = entry.rating.numberValue;
		if (!review.rating) {
			review.rating = [NSNumber numberWithInt:0];
		}
		
		review.updated = updatedDate;
        review.privacy = entry.privacy;
		
		// Multimedias
		NSArray *links = [entry linksWithRelAttributeValue: @"media"];
		
		for (GDataLink *link in links) {
			if ([link.type hasPrefix: @"image"]) {
				OosImage *image = [OosImage imageFromPath: link.href];
				[review.mutableMultimedias addObject: image];
			} else if ([link.type hasPrefix: @"video"]) {
				// TODO: not supported by the api yet
			}
		}
	}
	return review;
}

static NSString *schemeTemplate = @"http://api.11870.com/api/v2/%@/%@";

- (OosEntry *)reverseParseEntry:(OosObject *)object
{
    OosMe *me = [Oos4ios instance].me;
    if ([object isKindOfClass:[OosReview class]]) {
        OosReview *review = (OosReview *) object;
        
        OosServiceEntry *entry = (OosServiceEntry *) [[OosServiceEntryParser instance] 
                                                      reverseParseEntry:review.service];
        
        if ([object isKindOfClass:[OosReview class]]) {
            OosReview *review = (OosReview *)object;
            entry.summary = [GDataTextConstruct textConstructWithString: review.title];
            entry.content = [GDataEntryContent contentWithString: review.content];
            for (NSString *tag in review.tags) {
                NSString *scheme = [NSString stringWithFormat:schemeTemplate, 
                                    @"tags", me.slug];
                if (![tag isKindOfClass:[NSString class]]) {
                    tag = [tag description];
                }
                GDataCategory *category = [GDataCategory categoryWithScheme:scheme term:tag];
                [entry addCategory:category];
            }
            for (NSString *list in review.lists) {
                NSString *scheme = [NSString stringWithFormat:schemeTemplate, 
                                    @"lists", me.slug];
                if (![list isKindOfClass:[NSString class]]) {
                    list = [list description];
                }
                GDataCategory *category = [GDataCategory categoryWithScheme:scheme term:list];
                [entry addCategory:category];
            }
            if (review.rating) {
                OosRating *rating = [[OosRating new] autorelease];
                rating.numberValue = review.rating;
                entry.rating = rating;
            }
            entry.privacy = review.privacy;
            entry.updatedDate = [GDataDateTime dateTimeWithDate: [NSDate date] 
                                                       timeZone: [NSTimeZone systemTimeZone]];
            OosAuthor *author = [[OosAuthor new] autorelease];
            [author setName:me.nick];
            entry.author = author;
        }
        return entry;
    }
    return nil;
}



@end
